package com.jysz.certificate.web.action.pm;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.Constants;
import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.PerformanceAssignStatus;
import com.jysz.certificate.persistence.enums.PostType;
import com.jysz.certificate.persistence.model.pm.AssignUser;
import com.jysz.certificate.persistence.model.pm.PAShopMonthDetail;
import com.jysz.certificate.persistence.model.pm.PerformanceAssign;
import com.jysz.certificate.persistence.model.pm.PerformanceAssignShopDetail;
import com.jysz.certificate.service.pm.AssignUserService;
import com.jysz.certificate.service.pm.PAShopMonthDetailService;
import com.jysz.certificate.service.pm.PerformanceAssignService;
import com.jysz.certificate.service.pm.PerformanceAssignShopDetailService;
import com.jysz.certificate.utils.DateUtil;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/pm/assign")
public class PerformanceAssignShopDetailAction extends AbstractBaseAction  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PerformanceAssignShopDetail search;
	
	private PerformanceAssignShopDetail entity;
	private PAShopMonthDetail monthEntity;
	private PerformanceAssign parent;
	private AssignUser assignUser;
	
	@Resource
	private PerformanceAssignService performanceAssignService;
	
	@Resource
	private PerformanceAssignShopDetailService performanceAssignShopDetailService;
	
	@Resource
	private PAShopMonthDetailService paShopMonthDetailService;
	
	@Resource
	private AssignUserService assignUserService;
	
	private List<PAShopMonthDetail> paShopMonthDetails;
	
	private Map<Long, AssignUser> assignUserMaps;
	
	@Action(EDIT)
	public String edit() {
		parent = performanceAssignService.find(parent.getId());
		
		this.setPage(performanceAssignShopDetailService.findPageByPerformanceAssign(search, parent, this.getCurrentPage(), this.getPageSize()));
		
		return EDIT;
	}
	
	public boolean isCanAssign(PerformanceAssign object){
		Date today = DateUtil.getDate();
		
		return PerformanceAssignStatus.CONFIRM.equals(object.getStatus()) || DateUtil.isStartBeforeEndDate(object.getConfirmPlanDate(), today);
	}
	
	@Action("detail")
	public String detail() {
		parent = performanceAssignService.find(parent.getId());
		
		this.setPage(performanceAssignShopDetailService.findPageByPerformanceAssign(search, parent, this.getCurrentPage(), this.getPageSize()));
		
		return "detail";
	}
	
	@Action("editShopDetail")
	public String editShopDetail(){
		entity = performanceAssignShopDetailService.find(entity.getId());
		parent = entity.getPerformanceAssign();
		paShopMonthDetails = paShopMonthDetailService.findByShopDetail(entity);
		double amount = performanceAssignShopDetailService.findAreaTotalAmount(entity);
		this.getRequest().setAttribute("areaTotalAmount", amount);
		return "editShopDetail";
	}
	
	@Action("shopDetail")
	public String shopDetail(){
		entity = performanceAssignShopDetailService.find(entity.getId());
		parent = entity.getPerformanceAssign();
		paShopMonthDetails = paShopMonthDetailService.findByShopDetail(entity);
		double amount = performanceAssignShopDetailService.findAreaTotalAmount(entity);
		this.getRequest().setAttribute("areaTotalAmount", amount);
		return "shopDetail";
	}
	
	@Action("updateShopDetail")
	public String updateShopDetail(){
		PerformanceAssignShopDetail detail = performanceAssignShopDetailService.find(entity.getId());
		detail.setRemark(entity.getRemark());
		performanceAssignShopDetailService.save(detail);
		
		this.setSuccessPopMessage("保存成功");
		return this.editShopDetail();
	}
	
	@Action("editShopMonthDetail")
	public String editShopMonthDetail(){
		monthEntity = paShopMonthDetailService.find(monthEntity.getId());
		entity = monthEntity.getPerformanceAssignShopDetail();
		return "editShopMonthDetail";
	}
	
	@Action("updateShopMonthDetail")
	public String updateShopMonthDetail(){
		PAShopMonthDetail monthDetail = paShopMonthDetailService.find(monthEntity.getId());
		monthDetail.setRemark(monthEntity.getRemark());
		
		paShopMonthDetailService.saveShopMonthDetail(monthDetail, this.getAssignUserMaps(), monthEntity.getAreaTotalAmount(), this.getUser());
		
		this.setSuccessPopMessage("保存成功");
		return this.editShopMonthDetail();
	}
	
	@Action("addAssignUser")
	public String addAssignUser(){
		monthEntity = paShopMonthDetailService.find(monthEntity.getId());
		return "addAssignUser";
	}
	
	@Action("createAssignUser")
	public String createAssignUser(){
		monthEntity = paShopMonthDetailService.find(monthEntity.getId());
		
		if(PostType.Manager.equals(this.getUser().getPost())){
			assignUser.setDisplayColor(Constants.COLOR_RED);
		}else{
			if(PostType.Manager.equals(assignUser.getLastUpdatePost())){
				assignUser.setDisplayColor(Constants.COLOR_YELLOW);
			}
		}
		assignUser.setAddPost(this.getUser().getPost());
		assignUser.setLastUpdatePost(this.getUser().getPost());
		assignUser.setPaShopMonthDetail(monthEntity);
		
		assignUserService.saveCreateNewAssignUser(assignUser);
		
		setSuccessPopMessage("添加数据成功");
		setRedirectUrl("editShopMonthDetail.do?monthEntity.id=" + monthEntity.getId());
		return SHOW_MSG_PAGE;
	}

	@Action("destoryAssignUser")
	public String destoryAssignUser(){
		assignUser = assignUserService.find(assignUser.getId());
		Long monthEntityId = assignUser.getPaShopMonthDetail().getId();
		
		assignUserService.destoryAndUpdateAmount(assignUser);
		setSuccessPopMessage("删除数据成功");
		
		setRedirectUrl("editShopMonthDetail.do?monthEntity.id=" + monthEntityId);
		return SHOW_MSG_PAGE;
	}
	
	public PerformanceAssignShopDetail getSearch() {
		return search;
	}


	public void setSearch(PerformanceAssignShopDetail search) {
		this.search = search;
	}


	public PerformanceAssignShopDetail getEntity() {
		return entity;
	}


	public void setEntity(PerformanceAssignShopDetail entity) {
		this.entity = entity;
	}


	public PerformanceAssign getParent() {
		return parent;
	}


	public void setParent(PerformanceAssign parent) {
		this.parent = parent;
	}

	public List<PAShopMonthDetail> getPaShopMonthDetails() {
		return paShopMonthDetails;
	}

	public void setPaShopMonthDetails(List<PAShopMonthDetail> paShopMonthDetails) {
		this.paShopMonthDetails = paShopMonthDetails;
	}

	public PAShopMonthDetail getMonthEntity() {
		return monthEntity;
	}

	public void setMonthEntity(PAShopMonthDetail monthEntity) {
		this.monthEntity = monthEntity;
	}

	public Map<Long, AssignUser> getAssignUserMaps() {
		return assignUserMaps;
	}

	public void setAssignUserMaps(Map<Long, AssignUser> assignUserMaps) {
		this.assignUserMaps = assignUserMaps;
	}

	public AssignUser getAssignUser() {
		return assignUser;
	}

	public void setAssignUser(AssignUser assignUser) {
		this.assignUser = assignUser;
	}
	
}
